<?php
/**
 * User:于小涛
 * Date:2020/2/18
 * Time:16:31
 */
namespace app\admin\business\address;
use app\admin\model\address\County as CountyModel;

class County{

    private $obj_county_model=null;
    private $int_county_num=3;//每个层级的数量，2表示最多99
    public function __construct()
    {
        $this->obj_county_model = new CountyModel();
    }

    public function queryData($arr_where){
        return $this->obj_county_model->queryData($arr_where);
    }

    public function add($int_pid,$str_name,$float_sort){
        $arr_cid_cpid=$this->getCidCpidByParentId($int_pid);
        if($arr_cid_cpid['ret']!=0){
            return $arr_cid_cpid;
        }
        $arr_add_data=[
            'name'=>$str_name,
            'pid'=>$int_pid,
            'cid'=>$arr_cid_cpid['data']['cid'],
            'cpid'=>$arr_cid_cpid['data']['cpid'],
            'sort'=>$float_sort,
        ];
        $int_num = $this->obj_county_model->add($arr_add_data);
        if($int_num){
            return ['ret'=>0,'reason'=>'success'];
        }else{
            return ['ret'=>1,'reason'=>'创建失败'];
        }
    }

    public function del($int_id){
        $arr_child_list=$this->obj_county_model->where('pid',$int_id)->select()->toArray();
        if(!empty($arr_child_list)){
            return ['ret'=>1,'reason'=>'该栏目下有子栏目，不能够删除，请先删除子栏目'];
        }
        $int_num=$this->obj_county_model->del($int_id);
        if($int_num){
            return ['ret'=>0,'reason'=>'success'];
        }else{
            return ['ret'=>1,'reason'=>'删除失败'];
        }
    }

    public function edit($int_id,$str_name,$float_sort){
        $arr_edit=[
            'name'=>$str_name,
            'sort'=>$float_sort,
        ];
        $this->obj_county_model->edit($int_id,$arr_edit);
        return ['ret'=>0,'reason'=>'success'];
    }

    private function _maxCid(){
        return str_pad(9,$this->int_county_num,9,STR_PAD_RIGHT);
    }

    /**
     * 根据父级id获取当前子集的cid和cpid
     * User:于小涛
     * Date:2020/3/6
     * Time:18:12
     * @param int $int_pid
     * @return array
     */
    public function getCidCpidByParentId($int_pid = 0)
    {
        $arr_parent_info = $this->obj_county_model->findOrEmpty($int_pid)->toArray();
        $arr_re = ['ret'=>0,'reason'=>'success','data'=>['cid' => '', 'cpid' => '']];
        $arr_max_child_info = $this->obj_county_model->where('pid', $int_pid)->order('cid', 'desc')->limit(1)->findOrEmpty()->toArray();
        if (empty($arr_max_child_info)) {
            if (empty($arr_parent_info)) {
                $arr_re['data']['cid'] = str_pad(1, $this->int_county_num, '0', STR_PAD_RIGHT);
                $arr_re['data']['cpid'] = 0;
            } else {
                $arr_re['data']['cid'] = $arr_parent_info['cid'] . str_pad(1, $this->int_county_num, '0', STR_PAD_LEFT);
                $arr_re['data']['cpid'] = $arr_parent_info['cid'];
            }
        }else{
            $str_last3=substr($arr_max_child_info['cid'],-3);
            $str_last3=$str_last3*1+1;
            if($str_last3>$this->_maxCid()){
                $arr_re = ['ret'=>1,'reason'=>'子栏目已满','data'=>['cid' => '', 'cpid' => '']];
            }else{
                $arr_re['data']['cid'] = ltrim($arr_max_child_info['cpid'],0).$str_last3;
                $arr_re['data']['cpid'] = $arr_max_child_info['cpid'];
            }
        }
        return $arr_re;
    }

    public function getChildTreeData($int_id,&$arr_child_tree_data=[]){
        $arr_child_list=$this->obj_county_model->where('pid',$int_id)->select()->toArray();
        if(empty($arr_child_list)){
            return '';
        }
        $arr_child_tree_data=$arr_child_list;
        foreach($arr_child_tree_data as &$arr_item){
            $arr_item['value'] = $arr_item['id'];
            $arr_item['list']=[];
            $this->getChildTreeData($arr_item['id'],$arr_item['list']);
        }
    }

}